﻿using System.Collections.Generic;

namespace Maltose.MicroService
{
    /// <summary>
    /// 使用内容为 application/x-www-form-urlencoded 数据
    /// </summary>
    public class FormUrlEncodedData
    {
        /// <summary>
        /// 参数
        /// </summary>
        Dictionary<string, object> Pairs = new Dictionary<string, object>();

        /// <summary>
        /// 使用内容为 application/x-www-form-urlencoded 数据
        /// </summary>
        public FormUrlEncodedData() { }


        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void Add(string key, object value)
        {
            Pairs.TryAdd(key, value);
        }

        /// <summary>
        /// 格式数据
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return Pairs.ToFormData();
        }


    }

    /// <summary>
    /// 签名参数
    /// </summary>
    public class FormUrlSignEncodedData
    {

        /// <summary>
        /// 参数
        /// </summary>
        Dictionary<string, object> Pairs = new Dictionary<string, object>();

        /// <summary>
        /// 签名密钥
        /// </summary>
        public string ApiKey { get; set; }

        /// <summary>
        /// 签名密码
        /// </summary>
        public string Password { get; set; }


        /// <summary>
        /// 使用内容为 application/x-www-form-urlencoded 数据 带签名
        /// </summary>
        public FormUrlSignEncodedData()
        {

        }

        /// <summary>
        /// 使用内容为 application/x-www-form-urlencoded 数据 带签名
        /// </summary>
        /// <param name="apiKey">签名密钥</param>
        /// <param name="password">签名密码</param>
        public FormUrlSignEncodedData(string apiKey, string password)
        {
            ApiKey = apiKey;
            Password = password;
        }

        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void Add(string key, object value)
        {
            Pairs.TryAdd(key, value);
        }

        /// <summary>
        /// 返回签名后的参数
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            if (string.IsNullOrWhiteSpace(ApiKey))
            {
                return Pairs.ToMicroServiceData(ApiKey, Password, false);
            }
            return Pairs.ToMicroServiceData(ApiKey, Password);
        }
    }
}
